package model

import (
	"errors"
	"gin/common/types"
	Mysql "gin/database"
	teamones_helper "github.com/teamones-open/go-helper"
	"strings"
)

type Tools struct {
	Id          int64            `json:"id" gorm:"column:id"`
	Name        string           `json:"name" gorm:"column:name"`
	TenantId    int64            `json:"tenant_id" gorm:"column:tenant_id"`
	DCC         string           `json:"dcc" gorm:"column:dcc"`
	Category    string           `json:"category" gorm:"column:category"`
	Description string           `json:"description" gorm:"column:description"`
	FileName    string           `json:"file_name" gorm:"column:file_name"`
	Arguments   string           `json:"arguments" gorm:"column:arguments"`
	Icon        string           `json:"icon" gorm:"column:icon"`
	StyleSheet  string           `json:"style_sheet" gorm:"column:style_sheet"`
	Tips        string           `json:"tips" gorm:"column:tips"`
	Parent      string           `json:"parent" gorm:"column:parent"`
	Mode        string           `json:"mode" gorm:"column:mode"`
	Type        string           `json:"type" gorm:"column:type"`
	Created     types.TstampTime `json:"created" gorm:"column:created"`
	Uuid        string           `json:"uuid" gorm:"column:uuid"`
}

type ToolsFormat struct {
	Tools
	CategoryText string `json:"category_text" gorm:"column:category_text"`
	DCCText      string `json:"dcc_text" gorm:"column:dcc_text"`
	DccToolId    int64  `json:"dcc_tools_id" gorm:"column:dcc_tools_id"`
}

// 设置表名
func (Tools) TableName() string {
	return "tools"
}

// 插入工具数据
func (this *Tools) Insert() (id int64, err error) {
	result := Mysql.DB.Create(&this)
	id = this.Id
	if result.Error != nil {
		err = result.Error
		return
	}
	return
}

// 通过id查询一条用户数据
func (this *Tools) FindById(Id uint64) (resData Tools, err error) {
	resultFind := Mysql.DB.Where("id = ?", Id).First(&resData)

	if resultFind.Error != nil {
		err = resultFind.Error
		return
	}

	if resData.Id <= 0 {
		err = errors.New("Tool does not exist.")
	}
	return
}

// 更新百度网盘账户信息
func (this *Tools) Update() (err error) {
	if this.Id > 0 {
		resultUpdate := Mysql.DB.Save(&this)
		if resultUpdate.Error != nil {
			err = resultUpdate.Error
			return
		}
	} else {
		err = errors.New("ID must exist.")
	}

	return
}

// 通过id从移除工具
func (this *Tools) RemoveToolsById(id int64) (err error) {
	result := Mysql.DB.Where("id = ?", id).Delete(&this)

	if result.Error != nil {
		err = result.Error
	}
	return
}

// 多条查询
func (this *Tools) Select(Param teamones_helper.SelectQueryParam) (total int, resData []ToolsFormat, err error) {

	tools := make([]ToolsFormat, 0)
	db := Mysql.DB

	// 组装过滤条件
	if Param.Where != nil {
		for key, val := range Param.Where {
			vals := val.(map[string]interface{})
			if strings.Contains(key, "bw_") {
				db = db.Where(vals["condition"], vals["val1"], vals["val2"])
			} else {
				db = db.Where(vals["condition"], vals["val"])
			}
		}
	} else {
		db = db.Where("?", "1=1")
	}

	// 获取总条数
	db.Model(&Tools{}).Count(&total)

	// 查找所有数据

	// 应用分页
	db = db.Select(Param.Fields).Limit(Param.Limit).Offset(Param.Offset)

	// 应用排序参数
	if Param.Order != "" {
		db = db.Order(Param.Order)
	}

	result := db.Find(&tools)
	if result.Error != nil {
		err = result.Error
		return
	}

	return total, tools, err
}

// 通过id查找工具
func (this *Tools) SelectToolsListByIds(toolsIdsStr string) (resData []ToolsFormat, err error) {
	tools := make([]ToolsFormat, 0)
	db := Mysql.DB

	db = db.Where("id in (?)", strings.Split(toolsIdsStr, ","))

	result := db.Find(&tools)
	if result.Error != nil {
		err = result.Error
		return
	}

	return tools, err
}
